<?php

declare (strict_types=1);
/**
 * @copyright 深圳市易果网络科技有限公司
 * @version 1.0.0
 * @link https://dayiguo.com
 */
namespace App\Controller\user;

use App\Dao\AdvertisingDao;
use App\Dao\BlackListDao;
use App\Dao\CouponCategoryDao;
use App\Kernel\Util\UserJwtInstance;
use App\Service\User\BusinessService;
use App\Middleware\User\UserMiddleware;
use App\Middleware\User\UserNotNecessaryMiddleware;

use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\AutoController;
use Hyperf\HttpServer\Annotation\Middleware;

/**
 * 首页控制器
 *
 * @AutoController()
 * @Middleware(UserNotNecessaryMiddleware::class)
 * @author 刘兴永(aile8880@qq.com)
 * @package \App\Controller\user
 */
class MainController extends Controller
{
    /**
     * 广告 Dao
     *
     * @Inject
     * @var AdvertisingDao
     */
    private $advertising_dao;

    /**
     * 折扣券分类 Dao
     *
     * @Inject
     * @var CouponCategoryDao
     */
    private $coupon_category_dao;

    /**
     * @Inject
     * @var BusinessService
     */
    private $business_service;

    /**
     * @Inject
     * @var BlackListDao
     */
    private $black_list_dao;

    /**
     * 用户是否有注册区域
     */
    public function existUserRegisterAddress()
    {
        $user = UserJwtInstance::instance()->build()->getUser();

        $result = 0;
        if($user['province'] && $user['city'] && $user['district']) {
            $result = 1;
        }

        $this->success($result);
    }

    /**
     * 最新首页 -- 通过距离排序分页
     */
    public function homePage()
    {

        $longitudes = (float)$this->request->input('longitude', 114.082581);

        $latitudes = (float)$this->request->input('latitude', 22.624509);

        $page = (int)$this->request->input('page', '1');

        $longitude = $longitudes == 0 ?114.082581:$longitudes;

        $latitude = $latitudes == 0 ?22.624509:$latitudes;

        // 区定位ID
        $location_id = (int)$this->request->input('location_id', 0);
        $location_ids = $this->business_service->locationsArray($location_id);

        // 首页轮播广告
        $banner = $this->advertising_dao->getAdsByTypeAndLocations(1, $location_ids);
//        $banner = $this->advertising_dao->getAdvertisingByType(1);
        // 附近商家
        $shop = $this->business_service->homePage($longitude,$latitude,$page);

        $this->success([
            'banner' => $banner,
            'shop' => $shop
        ]);
    }

    /**
     * 首页金刚区 - 通过距离排序分页
     */
    public function iconNewPage()
    {
        $industry_id = (int)$this->request->input('industry_id', 0);

        $longitude = (float)$this->request->input('longitude', 114.082581);

        $latitude = (float)$this->request->input('latitude', 22.624509);

        $page = (int)$this->request->input('page', '1');

        $distance = (int)$this->request->input('distance', 0);

        $type = (int)$this->request->input('type', 0);

        $sort_type = (int)$this->request->input('sort_type', 0);

        $result = $this->business_service->iconNewPage( $industry_id, $longitude, $latitude, $page, $distance, $type, $sort_type );

        $this->success($result);

    }

    /**
     * 审核模式
     */
    public function Examine()
    {

        $user = UserJwtInstance::instance()->build()->getUser();

        $data = $this->black_list_dao->find($user['phone']);

        $result = $data ? "1" : gConfig('examine') ;

        $this->success($result);
    }

    /**
     * 首页搜索
     */
    public function search()
    {
        $longitude = (float)$this->request->input('longitude', 114.082581);

        $latitude = (float)$this->request->input('latitude', 22.624509);

        $content = (string)$this->request->input('content', "");

        $page = (int)$this->request->input('page', '1');

        $distance = (int)$this->request->input('distance', 0);

        $type = (int)$this->request->input('type', 0);

        $sort_type = (int)$this->request->input('sort_type', 0);

        $result =  $this->business_service->search($content, $longitude, $latitude, $page, $distance, $type, $sort_type);

        $this->success($result);
    }

    /**
     * 首页综合接口 - 老版本
     */
    public function index()
    {
        $longitude = (float)$this->request->input('longitude', 114.082581);

        $latitude = (float)$this->request->input('latitude', 22.624509);

        // 首页轮播广告
        $banner = $this->advertising_dao->getAdvertisingByType(1);

        // 附近商家
        $shop = $this->business_service->main( $longitude,$latitude);

        $this->success([
            'banner' => $banner,
            'shop' => $shop
        ]);
    }

    /**
     * 首页综合接口 - 新版本
     */
    public function main()
    {
        $longitudes = (float)$this->request->input('longitude', 114.082581);

        $latitudes = (float)$this->request->input('latitude', 22.624509);

        $page = (int)$this->request->input('page', '1');

        $longitude = $longitudes == 0 ?114.082581:$longitudes;

        $latitude = $latitudes == 0 ?22.624509:$latitudes;

        // 首页轮播广告
        $banner = $this->advertising_dao->getAdvertisingByType(1);
        // 附近商家
        $shop = $this->business_service->index($longitude,$latitude,$page);

        $this->success([
            'banner' => $banner,
            'shop' => $shop
        ]);
    }

    /**
     * 店铺
     */
    public function store()
    {
        $business_id = (int)$this->request->input('business_id', 0);

        $longitude = (float)$this->request->input('longitude', 114.082581);

        $latitude = (float)$this->request->input('latitude', 22.624509);

        $result = $this->business_service->store( $business_id, $longitude, $latitude );

        $this->success($result);
    }

    /**
     * 首页金刚区 - 老版本
     */
    public function icon()
    {
        $industry_id = (int)$this->request->input('industry_id', 0);

        $longitude = (float)$this->request->input('longitude', 114.082581);

        $latitude = (float)$this->request->input('latitude', 22.624509);


        $result = $this->business_service->icon( $industry_id, $longitude, $latitude );

        $this->success($result);

    }

    /**
     * 首页金刚区 - 新版本
     */
    public function iconNew()
    {
        $industry_id = (int)$this->request->input('industry_id', 0);

        $longitude = (float)$this->request->input('longitude', 114.082581);

        $latitude = (float)$this->request->input('latitude', 22.624509);

        $page = (int)$this->request->input('page', '1');

        $result = $this->business_service->iconNew( $industry_id, $longitude, $latitude, $page );

        $this->success($result);

    }

    /**
     * 热词列表
     */
    public function hot_words_list()
    {
        $this->success(gConfig('hot_search'));
    }
}